layui.use(['layer', 'laypage', 'element', 'form', 'upload', 'laydate'], function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer,
        upload = layui.upload;

    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块

    //表单
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
    //时间组件
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
        elem: '#birth' //指定元素
    });
    $("#but1").click(function () {
        $.ajax({
            url: '/User/update',
            type: "post",
            data: $("#form1").serialize(),
            success: function (data) {
                if (data.msg == "修改成功!") {
                    setTimeout(function () {
                        window.location.reload();//刷新当前页面.
                    }, 1000);
                }
                layer.msg(data.msg);
            },
            error: function (data) {
                layer.msg(data.msg);
            }
        });
    });
    //取消关注
    follow = function (obj) {
        var did = $(obj).attr("value");
        $.ajax({
            url: "/User/delfollow",
            data: {did: did},
            success: function (data) {
                layer.msg(data.msg);
                $(obj).parent().remove();
            }
        });
    };
    //关注
    fan = function (obj) {
        var did = $(obj).attr("value");
        $.ajax({
            url: "/User/addfollow",
            data: {"did": did},
            success: function (data) {
                layer.msg(data.msg);
            }
        });
    };

    //进入收藏
    $(".collection-content").find("li").click(function () {
        $.ajax({
            url: '/User/getcollection',
            success(data) {
                $(".collection-list").find("b").text(data.blogs.length + "条内容");
                for (var o in data.collections) {
                    $(".collection-items").append(
                        "<li  value=" + data.collections[o]["bld"] + ">" +
                        "<div class='collection-item'>" +
                        "<span>" +
                        "<a class='item-title'  href='/Blog/blogdetail/" + data.blogs[o]["bid"] + "'>" + data.blogs[o]["title"] + "</a>" +
                        "<input type='checkbox' class='sel' />" +
                        "<a class='item-right' onclick='delcol(this);' value=" + data.collections[o]["bld"] + ">取消收藏</a>" +
                        "</span>" +
                        "<span>" +
                        "<em class='lline'></em>" +
                        "<b class='collection-line' >" + data.blogs[o]["date"] + "</b>" +
                        "</span>" +
                        "</div>" +
                        "</li>"
                    );
                }
                batch();
            }
        });
        $("#header-block").hide("slow");
        $(".collection-content").hide("slow");
        $("#header-none").show("slow");
        $(".collection-list").show("slow");
    });
    $("#header-none").click(function () {
        $(".collection-items").empty();
        $("#header-block").show("slow");
        $(".collection-content").show("slow");
        $("#header-none").hide("slow");
        $(".collection-list").hide("slow");
        $(".nothing").fadeOut("slow");

    });
    //取消收藏
    delcol = function (obj) {
        $.ajax({
            url: '/User/delcol/' + $(obj).attr("value"),
            success(data) {
                $(obj).parent().parent().parent().hide("slow");
                layer.msg(data);
                setTimeout(function () {
                    $(obj).parent().parent().parent().remove();
                    if ($(".collection-items li").length == 0) {
                        $(".collection-list").hide();
                        $(".nothing").fadeIn("slow");
                    }
                }, 500);

            }
        });

    };
    //批量操作
    // cleancol = function (obj) {
    // var li = "";
    // var checkson = $(".sel");
    // for (var i = 0; i < checkson.length; i++) {
    //     if(checkson[i].checked == true);
    //     li += $(this).next().attr("value") + ",";
    //     console.log($(this).html())
    // }
    // console.log(li);
    // $(".collection-items").empty();
    // $.ajax({
    //     url: '/User/cleancol',
    //     data: {li: li},
    //     success(data) {
    //         layer.msg(data);
    //     }
    // });
    //     if($(".collection-items li").length==0){
    //         $(".collection-list").hide();
    //         $(".nothing").fadeIn("slow");
    //     }
    // };

    batch();

    function batch() {
        var checked = $("#select_all")[0];
        var checkson = $(".sel");
        $(".all_select").find("span").click(function () {
            if ($("#select_all")[0].checked == false) {
                $("#select_all")[0].checked = true;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = true;
                }
            } else {
                $("#select_all")[0].checked = false;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = false;
                }
            }
        });
        $("#select_all").click(function () {
            if ($("#select_all")[0].checked == false) {
                $("#select_all")[0].checked = false;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = false;
                }
            } else {
                $("#select_all")[0].checked = true;
                for (var i = 0; i < checkson.length; i++) {
                    checkson[i].checked = true;
                }
            }
        });
    }

    //新建收藏夹
    $(".user-collection .new-collection").click(function () {
        layer.open({
            type: 1,
            content: '传入任意的文本或html' //这里content是一个普通的String
        });
    });

});